//
//  GKZYHomeTabBarViewController.m
//  GKZY_CLASS
//
//  Created by KwokChun Cheung on 15/12/1.
//  Copyright © 2015年 eenet. All rights reserved.
//
#import <JGProgressHUD/JGProgressHUD.h>

#import "GKZYHomeTabBarViewController.h"
#import "GKZYHomeViewController.h"
#import "GKZYMyInfoViewController.h"
#import "GKZYCourseListReq.h"
#import "GKZY_CLASS.h"


@interface GKZYHomeTabBarViewController ()

@end

@implementation GKZYHomeTabBarViewController{
    JGProgressHUD *progress;
    
    GKZYHomeViewController *homeVC;
    GKZYMyInfoViewController *infoVC;
    BOOL isFirstShow;
}

//-(UIStatusBarStyle)preferredStatusBarStyle{
//    return UIStatusBarStyleLightContent;
//}

-(instancetype)initWithSid:(NSString *)sid{
    if (self = [super init]) {
        self.sid = sid;
        
//        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
        
        isFirstShow = YES;
        progress = [[JGProgressHUD alloc]initWithStyle:JGProgressHUDStyleDark];
        
        CGFloat sizeForIcon = 26;
        
        homeVC = [[GKZYHomeViewController alloc]initWithSid:self.sid];
        [homeVC.tabBarItem setTitle:@"课程"];
        [homeVC.tabBarItem setImage:[self imageWithText:@"A" font:[UIFont fontWithName:OO_FONT_NAME size:sizeForIcon] iconColor:[UIColor blackColor] imageSize:CGSizeMake(sizeForIcon, sizeForIcon)]];
        
        infoVC = [GKZYMyInfoViewController new];
        [infoVC.tabBarItem setTitle:@"我"];
        [infoVC.tabBarItem setImage:[self imageWithText:@"G" font:[UIFont fontWithName:OO_FONT_NAME size:sizeForIcon] iconColor:[UIColor blackColor] imageSize:CGSizeMake(sizeForIcon, sizeForIcon)]];
        UINavigationController *infoNavi = [[UINavigationController alloc]initWithRootViewController:infoVC];
        
        self.viewControllers = @[homeVC, infoNavi];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    
    if (!isFirstShow) {
        GKZYCourseListReq *courseReq = [GKZYCourseListReq new];
        courseReq.sid = homeVC.sid;
        courseReq.cid = homeVC.currentCid;
        courseReq.pageNo = [homeVC.currentPage stringValue];
        courseReq.pageSize = OO_PAGE_NO;
        
        [homeVC requestList:courseReq isAdd:NO onLoading:YES];
    }
    
    isFirstShow = NO;
}

#pragma mark - Tools Methods

/**
 * 图标转图片
 **/
-(UIImage*)imageWithText:(NSString*)characterCodeString font:(UIFont*)font iconColor:(UIColor*)iconColor imageSize:(CGSize)imageSize{
    NSAssert(characterCodeString, @"必须有内容，比如$");
    if (!iconColor){
        iconColor = [UIColor blackColor];
    }
    
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:characterCodeString attributes:@{NSFontAttributeName:font, NSForegroundColorAttributeName:iconColor}];
    
    //生成目标区域 用来居中图片
    NSStringDrawingContext *ctx = [[NSStringDrawingContext alloc] init];
    CGRect boundingRect = [attString boundingRectWithSize:CGSizeMake(font.pointSize, font.pointSize) options:NSStringDrawingUsesLineFragmentOrigin context:ctx];
    
    //往图片里画图标
    [attString drawInRect:CGRectMake((imageSize.width/2.0f) - boundingRect.size.width/2.0f,
                                     (imageSize.height/2.0f) - boundingRect.size.height/2.0f,
                                     imageSize.width,
                                     imageSize.height)];
    UIImage *iconImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return iconImage;
}

@end
